//
//  MPTLiveZuiXinCell.m
//  MPTLive
//
//  Created by HouGeng on 16/3/1.
//  Copyright © 2016年 Jeakin. All rights reserved.
//

#import "MPTLiveZuiXinCell.h"

// 商品列表间距
#define GMM_ListKong (1.5)

#define GMM_ImageViewHW ((screenW - 6) / 3.0)

@implementation MPTLiveZuiXinCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setAryData:(NSArray *)aryData
{
    if (_aryData != aryData)
    {
        _aryData = aryData;
        
        // 移除所有试图
        //删除之前的布局
        [self.contentView.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
         {
             if ([obj isKindOfClass:[UIImageView class]])
             {
                 [obj removeFromSuperview];
             }
         }];
        
        /// 加数据保护
        if ([aryData isKindOfClass:[NSArray class]])
        {
            TPImageView *imageV;
            for (NSInteger i = 0; i < aryData.count; i++)
            {
                imageV = [[TPImageView alloc] initWithFrame:CGRectMake((GMM_ListKong * (i + 1)) + i *GMM_ImageViewHW,1.5,GMM_ImageViewHW,GMM_ImageViewHW)];
                imageV.tag = i;
                [imageV addTarget:self action:@selector(clocked:)];
                if ([aryData[i] isKindOfClass:[NSDictionary class]])
                {
                    /// 获取数据
                    NSDictionary *dic = aryData[i][@"channel"];
                    /// 大图
                    [imageV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",dic[@"pic"][@"base"],dic[@"pic"][@"m"]]] placeholderImage:[UIImage imageNamed:@"listQueSheng"]];
                    
                    /// 视频状态
                    if ([dic[@"liveStatus"] integerValue] == 1)
                    {
                        UIImage *image = [UIImage imageNamed:@"imaLive"];
                        TPImageView *imaV = [TPImageView getWithFrame:CGRectMake(GMM_ImageViewHW-5 - image.size.width, 5, image.size.width, image.size.height) image:image];
                        [imageV addSubview:imaV];
                    }
                    else
                    {
                        UIImage *image = [UIImage imageNamed:@"imaLuBo"];
                        TPImageView *imaV = [TPImageView getWithFrame:CGRectMake(GMM_ImageViewHW-5 - image.size.width, 5, image.size.width, image.size.height) image:image];
                        [imageV addSubview:imaV];
                    }
                }
                [self.contentView addSubview:imageV];
            }
        }
    }
    
}

- (void)clocked:(TPImageViewBtn *)sender
{
    if (self.block)
    {
        self.block(_indexpath, sender.imgv.tag);
    }
}

@end
